// part1:C10:ex10.18.cpp
// use partition to replace find_if.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include "make_plural.h"
using namespace std;
void elimDups(vector<string>& words)
{
  sort(words.begin(), words.end());
  auto end_unique = unique(words.begin(), words.end());
  words.erase(end_unique, words.end());
}

void biggies(vector<string>& words,
             vector<string>::size_type sz)
{
  elimDups(words);
  for_each(words.begin(), words.end(),
           [](const string& s){
             cout << s << " ";
           });
  cout << endl;
  auto wc = partition(words.begin(), words.end(),
                      [sz](const string& s1){
                        return s1.size() >= sz;
                      });

  auto count = wc - words.begin();
  cout << count << " " << make_plural(count, "words", "s")
       << " of length " << sz << " or longer" << endl;

  for_each(words.begin(), wc,
           [](const string& s){ cout << s << " "; });

  cout << endl;
}

int main(int argc, char *argv[]) 
{
  ifstream in(argv[1]);
  if (!in)
  {
    cout << "failed open input file!";
    exit(1);
  }

  vector<string> words;
  string word;
  while (in >> word)
  {
    words.push_back(word);
  }

  biggies(words, 4);

  return 0;
}
